package com.ler.nacosdubbo.config;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lww
 * @date 2019-08-17 15:37
 */
@Configuration
//扫描生产者service
@DubboComponentScan(basePackages = "com.ler.nacosdubbo.service.impl")
public class DubboConfiguration {

	/**
	 * 当前应用配置
	 */
	@Bean
	public ApplicationConfig applicationConfig() {
		ApplicationConfig applicationConfig = new ApplicationConfig();
		//应用名字
		applicationConfig.setName("nacos-dubbo");
		Map<String, String> parameters = new HashMap<>(16);
		//qos打开，会造成端口冲突
		parameters.put("qos.enable", "false");
		applicationConfig.setParameters(parameters);
		return applicationConfig;
	}

	/**
	 * 连接注册中心配置
	 */
	@Bean
	public RegistryConfig registryConfig() {
		RegistryConfig registryConfig = new RegistryConfig();

		//zk 协议
		//registryConfig.setProtocol("zookeeper");
		//zk 地址
		//registryConfig.setAddress("127.0.0.1:2181");

		//nacos 协议
		registryConfig.setProtocol("nacos");
		//nacos 地址
		registryConfig.setAddress("127.0.0.1:8848");

		//缓存存放路径
		registryConfig.setFile("dubbo.cache");
		//whether to export service on the register center
		registryConfig.setRegister(true);
		registryConfig.setClient("curator");
		return registryConfig;
	}

	/**
	 * 服务提供者协议配置
	 */
	@Bean
	public ProtocolConfig protocolConfig() {
		ProtocolConfig protocolConfig = new ProtocolConfig();
		//协议名字 还有其他的，感兴趣的可以去搜一下看看 rmi协议，hessian协议，thrift协议，http协议
		protocolConfig.setName("dubbo");
		//service port，-1 随机端口
		protocolConfig.setPort(-1);
		//thread pool size (fixed size)
		protocolConfig.setThreads(100);
		return protocolConfig;
	}

	/**
	 * 消费者统一设置
	 */
	@Bean
	public ConsumerConfig consumerConfig() {
		ConsumerConfig consumerConfig = new ConsumerConfig();
		//超时时间
		consumerConfig.setTimeout(3000);
		//启动检查
		consumerConfig.setCheck(false);
		//懒加载
		consumerConfig.setLazy(true);
		return consumerConfig;
	}

}
